package com.api.shopapi.controller;


import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ActivityContentDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 活动内容
 * */

@RestController
@RequestMapping("ActivityContent")
public class ActivityContentController extends BaseController {


    @Autowired
    @Qualifier("ActivityContentServiceImplOne")
    private ActivityContentService activityContentService;

    @Autowired
    @Qualifier("CommandCenterStaffRelServiceImplOne")
    private CommandCenterStaffRelService commandCenterStaffRelService;


    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    @Autowired
    @Qualifier("ActivityTypeServiceImplOne")
    private ActivityTypeService activityTypeService;


    @Autowired
    @Qualifier("ActivityItemServiceImplOne")
    private ActivityItemService activityItemService;



    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("name");
            String activityTypeCode = jsonObject.getString("activityTypeCode");
            String activityItemCode = jsonObject.getString("activityItemCode");
            int frequency = jsonObject.getInteger("frequency");

            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityContent/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            ActivityContent activityContent =new ActivityContent();
            activityContent.setActivityContentCode(UUID.randomUUID().toString());
            activityContent.setActivityTypeCode(activityTypeCode);
            activityContent.setActivityItemCode(activityItemCode);
            activityContent.setName(name);
            activityContent.setStatus(1);
            activityContent.setFrequency(frequency);
            activityContent.setGovernmentCode(staff.getGovernmentCode());
            activityContent.setCommandCenterCode(commandCenterStaffRel.getCommandCenterCode());
            activityContentService.insert(activityContent);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityContent/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("name");
            String activityTypeCode = jsonObject.getString("activityTypeCode");
            int status = jsonObject.getInteger("status");
            String activityItemCode = jsonObject.getString("activityItemCode");
            int frequency = jsonObject.getInteger("frequency");

            String staffCode = jsonObject.getString("staffCode");

            String activityContentCode = jsonObject.getString("activityContentCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityContent/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ActivityContent activityContent = activityContentService.getByActivityContentCode(activityContentCode);
            activityContent.setName(name);
            activityContent.setStatus(status);
            activityContent.setActivityTypeCode(activityTypeCode);
            activityContent.setActivityItemCode(activityItemCode);
            activityContent.setFrequency(frequency);
            activityContentService.update(activityContent);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityContent/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult getList(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String activityItemCode = jsonObject.getString("activityItemCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityContent/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<ActivityContent> list = activityContentService.ListByActivityItemCode(activityItemCode);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",list);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityContent/List",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //指挥中心获取活动内容翻页列表
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            String activityTypeCode = jsonObject.getString("activityTypeCode");
            String activityItemCode = jsonObject.getString("activityItemCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityContent/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            PageInfo<ActivityContent> pageInfo = activityContentService.pageByCommandCenterCode(pageIndex,pageSize,
                    commandCenterStaffRel.getCommandCenterCode(),activityTypeCode,activityItemCode);

            List<ActivityContentDTO> listDTO = new ArrayList<>();
            for(ActivityContent activityContent:pageInfo.getList()){

                ActivityContentDTO activityContentDTO = mapper.map(activityContent,ActivityContentDTO.class);

                ActivityType activityType = activityTypeService.getByActivityTypeCode(activityContent.getActivityTypeCode());
                activityContentDTO.setActivityTypeName(activityType.getName());

                ActivityItem activityItem = activityItemService.getByActivityItemCode(activityContent.getActivityItemCode());
                activityContentDTO.setActivityItemName(activityItem.getName());

                listDTO.add(activityContentDTO);
            }

            PageInfo<ActivityContentDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo1);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityContent/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //小程序获取指挥中心下活动内容翻页列表
    @RequestMapping(value = "PageXCX",method = RequestMethod.POST)
    public MyResult PageXCX(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            String commandCenterCode = jsonObject.getString("commandCenterCode");
            String activityTypeCode = jsonObject.getString("activityTypeCode");
            String activityItemCode = jsonObject.getString("activityItemCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/ActivityContent/PageXCX");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PageInfo<ActivityContent> pageInfo = activityContentService.pageByCommandCenterCode(pageIndex,pageSize,
                    commandCenterCode,activityTypeCode,activityItemCode);

            List<ActivityContentDTO> listDTO = new ArrayList<>();
            for(ActivityContent activityContent:pageInfo.getList()){

                ActivityContentDTO activityContentDTO = mapper.map(activityContent,ActivityContentDTO.class);

                ActivityType activityType = activityTypeService.getByActivityTypeCode(activityContent.getActivityTypeCode());
                activityContentDTO.setActivityTypeName(activityType.getName());

                ActivityItem activityItem = activityItemService.getByActivityItemCode(activityContent.getActivityItemCode());
                activityContentDTO.setActivityItemName(activityItem.getName());

                listDTO.add(activityContentDTO);
            }

            PageInfo<ActivityContentDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo1);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityContent/PageXCX", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


}
